﻿//-----------------------------------------------------------------------------
// <copyright file="Card.cs" company="Company">
//  What does one write here?
// </copyright>
//-----------------------------------------------------------------------------

namespace Cards
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    /// <summary>
    /// A class representing a single card
    /// </summary>
    public sealed class Card
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Card"/> class.
        /// </summary>
        /// <param name="rank">The rank of the card.</param>
        /// <param name="suit">The suit of the card.</param>
        public Card(Rank rank, Suit suit)
        {
            if (!Enum.IsDefined(typeof(Rank), rank))
            {
                throw new ArgumentOutOfRangeException("rank", string.Format("The value {0} is not defined in the enum \"{1}\"", rank, typeof(Rank).FullName));
            }

            if (!Enum.IsDefined(typeof(Suit), suit))
            {
                throw new ArgumentOutOfRangeException("suit", string.Format("The value {0} is not defined in the enum \"{1}\"", suit, typeof(Suit).FullName));
            }

            this.Rank = rank;
            this.Suit = suit;
        }

        /// <summary>
        /// Gets or sets the rank of the card.
        /// </summary>
        /// <value>The rank of the card.</value>
        public Rank Rank { get; set; }

        /// <summary>
        /// Gets or sets the suit of the card.
        /// </summary>
        /// <value>The suit of the card.</value>
        public Suit Suit { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            string rank;
            char suit;
            if (this.Rank <= Rank.Ten)
            {
                rank = ((int)this.Rank).ToString();
            }
            else
            {
                rank = Enum.GetName(typeof(Rank), this.Rank).Substring(0, 1);
            }

            suit = Enum.GetName(typeof(Suit), this.Suit)[0];

            return string.Concat(rank, "-", suit);
        }
    }

    public static class CardHelper
    {
        public static string GetString(this ICollection<Card> cards)
        {
            return string.Join(", ", cards.Select(card => card.ToString()).ToArray());
        }
    }
}